package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.FreeDeliveryTermsConvert;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.FreeDeliveryTermsDtoMapper;
import com.starlink.model.domain.FreeDeliveryTermsDO;
import com.starlink.mapper.FreeDeliveryTermsMapper;
import com.starlink.model.request.FreeDeliveryTermsRequest;
import com.starlink.service.FreeDeliveryTermsService;
import com.starlink.model.vo.FreeDeliveryTermsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class FreeDeliveryTermsServiceImpl extends ServiceImpl<FreeDeliveryTermsMapper, FreeDeliveryTermsDO> implements FreeDeliveryTermsService {
    @Autowired
    private FreeDeliveryTermsMapper freeDeliveryTermsMapper;

    @Override
    public FreeDeliveryTermsVO getByKey(long id) {
        FreeDeliveryTermsVO freeDeliveryTermsVO = FreeDeliveryTermsDtoMapper.INSTANCE.do2VO(getById(id));
        return freeDeliveryTermsVO;
    }

    @Override
    public PageInfo<FreeDeliveryTermsVO> pageQuery(Pageable pageable,FreeDeliveryTermsRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<FreeDeliveryTermsDO> list = freeDeliveryTermsMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<FreeDeliveryTermsVO> freeDeliveryTermsVOList = FreeDeliveryTermsConvert.buildFreeDeliveryTermsVoList(list);
        return new PageInfo(freeDeliveryTermsVOList);
    }

    @Override
    public List<FreeDeliveryTermsVO> listQuery(FreeDeliveryTermsRequest request) {
        List<FreeDeliveryTermsDO> list = freeDeliveryTermsMapper.selectDOList(FreeDeliveryTermsDtoMapper.INSTANCE.request2DO(request));
        List<FreeDeliveryTermsVO> freeDeliveryTermsVOS = FreeDeliveryTermsConvert.buildFreeDeliveryTermsVoList(list);
        return freeDeliveryTermsVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(FreeDeliveryTermsRequest request) {
        save(FreeDeliveryTermsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(FreeDeliveryTermsRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(FreeDeliveryTermsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        freeDeliveryTermsMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<FreeDeliveryTermsDO> termsDOs) {
        freeDeliveryTermsMapper.batchInsertDO(termsDOs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByTempId(Long templateId) {
        freeDeliveryTermsMapper.deletedByTempId(templateId);
    }
}
